//
//  HLHomeVideoCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/31.
//

#import "HLHomeVideoCell.h"
#import "HLUserDetailViewController.h"

@interface HLHomeVideoCell ()
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIVisualEffectView *backImageView;
@property (nonatomic, strong) UIImageView *bottomBlurView;
@property (nonatomic, strong) QMUIButton *avatarView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) QMUIButton *likeButton;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

@end

@implementation HLHomeVideoCell


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorBlack;
        self.layer.masksToBounds = YES;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundImageView = [[UIImageView alloc] init];
    _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_backgroundImageView];
    [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.backImageView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect qmui_effectWithBlurRadius:30]];
    [self.contentView addSubview:_backImageView];
    [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.playerSuperview = [[UIView alloc] init];
    [self.contentView addSubview:_playerSuperview];
    [_playerSuperview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.bottomBlurView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_cover")];
    _bottomBlurView.contentMode = UIViewContentModeScaleAspectFill;
    _bottomBlurView.hidden = YES;
    [self.contentView addSubview:_bottomBlurView];
    
    self.playButton = [[QMUIButton alloc] init];
    [_playButton setImage:UIImageMake(@"user_detail_cell_play") forState:UIControlStateNormal];
    [self.contentView addSubview:_playButton];
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    self.avatarView = [[QMUIButton alloc] init];
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 20;
    _avatarView.layer.borderWidth = 1;
    _avatarView.layer.borderColor = UIColorWhite.CGColor;
    [_avatarView addTarget:self action:@selector(profileAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(40);
        make.left.mas_equalTo(12);
        make.bottom.mas_equalTo(-36);
    }];
    
    self.nicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(14) textColor:UIColorWhite];
    _nicknameLabel.text = @"123";
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_avatarView);
        make.left.equalTo(_avatarView.mas_right).offset(10);
    }];
    
    self.followButton = [[QMUIButton alloc] init];
    [_followButton setTitle:@"+" forState:UIControlStateNormal];
    [_followButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_followButton.titleLabel setFont:UIFontBoldMake(12)];
    [_followButton setBackgroundColor:HLMainColor];
    [_followButton addTarget:self action:@selector(followAction:) forControlEvents:UIControlEventTouchUpInside];
    [_followButton setContentEdgeInsets:UIEdgeInsetsMake(2, 12, 2, 12)];
    _followButton.cornerRadius = 8;
    [self.contentView addSubview:_followButton];
    [_followButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_avatarView.mas_bottom);
        make.centerX.equalTo(_avatarView);
    }];
    
    self.videoButton = [[QMUIButton alloc] init];
    [_videoButton setImage:UIImageMake(@"home_video_rtc") forState:UIControlStateNormal];
    [_videoButton setTitle:@"视频" forState:UIControlStateNormal];
    [_videoButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_videoButton.titleLabel setFont:UIFontBoldMake(12)];
    _videoButton.imagePosition = QMUIButtonImagePositionTop;
    _videoButton.spacingBetweenImageAndTitle = 6;
    [self.contentView addSubview:_videoButton];
    [_videoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(_avatarView);
    }];
    
    self.likeButton = [[QMUIButton alloc] init];
    [_likeButton setImage:UIImageMake(@"home_video_like") forState:UIControlStateNormal];
    [_likeButton setImage:UIImageMake(@"home_video_like_hl") forState:UIControlStateSelected];
    [_likeButton setTitle:@"366" forState:UIControlStateNormal];
    [_likeButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_likeButton.titleLabel setFont:UIFontBoldMake(12)];
    [_likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    _likeButton.imagePosition = QMUIButtonImagePositionTop;
    _likeButton.spacingBetweenImageAndTitle = 6;
    [self.contentView addSubview:_likeButton];
    [_likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_videoButton);
        make.right.equalTo(_videoButton.mas_left).offset(-40);
    }];
    
    
    
    self.contentLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(14) textColor:UIColorWhite];
    _contentLabel.text = @"199";
    _contentLabel.numberOfLines = 0;
    _contentLabel.qmui_lineHeight = 20;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_avatarView.mas_top).offset(-20);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
    }];
    
    
    if (![HLRunTime shareInstance].isFood){
        
        self.wantButton = [[QMUIButton alloc] init];
        [_wantButton setImage:UIImageMake(@"want_video_icon") forState:UIControlStateNormal];
        [_wantButton setTitle:@"邀请你视频" forState:UIControlStateNormal];
        [_wantButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
        [_wantButton.titleLabel setFont:UIFontMediumMake(16)];
        [_wantButton setBackgroundColor:HLMainColor];
        _wantButton.layer.masksToBounds = YES;
        _wantButton.layer.cornerRadius = 30;
        _wantButton.layer.borderColor = [UIColorWhite colorWithAlphaComponent:0.6].CGColor;
        _wantButton.layer.borderWidth = 3;
        _wantButton.hidden = YES;
        _wantButton.spacingBetweenImageAndTitle = 8;
        _wantButton.imagePosition = QMUIButtonImagePositionLeft;
        [self.contentView addSubview:_wantButton];
        [_wantButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(200, 60));
            make.centerX.mas_equalTo(0);
            make.bottom.equalTo(_contentLabel.mas_top).offset(-30);
        }];
    }
    
    
    [_bottomBlurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(_contentLabel).offset(-12);
    }];
    
}

- (void)setModel:(HLUserVideoModel *)model
{
    _model = model;
    [_backgroundImageView sd_setImageWithURL:[NSURL URLWithString:model.imgLink] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] forState:UIControlStateNormal placeholderImage:[HLUIHelper avatarPlaceholder]];
    _nicknameLabel.text = model.nickName;
    _contentLabel.text = model.videoDesc;
    [_likeButton setTitle:model.thumbUpCount forState:UIControlStateNormal];
    _likeButton.selected = [model.isThumbUp boolValue];
    _followButton.hidden = [model.isFollow boolValue];
    [self stopVideo];
}

- (void)playVideo
{
    _playButton.hidden = YES;
}


- (void)stopVideo
{
    _playButton.hidden = NO;
    
}

- (void)invaiteVideo
{
    if (self.model.isWantVideo){
        self.wantButton.hidden = YES;
    } else {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.model.isWantVideo = YES;
            [self animateWithWant];
            self.wantButton.hidden = NO;
        });
    }
}

- (void)animateWithWant
{
    if ([HLRunTime shareInstance].isFood){
        return;
    }
    
    [self.wantButton.layer removeAllAnimations];
    CABasicAnimation*posAni = [CABasicAnimation animationWithKeyPath:@"position"];
    posAni.toValue=[NSValue valueWithCGPoint:self.wantButton.center];
    CABasicAnimation*opcAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opcAni.toValue=[NSNumber numberWithFloat:0.8];
    CABasicAnimation*bodAni = [CABasicAnimation animationWithKeyPath:@"bounds"];
    bodAni.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 202, 62)];
    CAAnimationGroup *groupAni = [CAAnimationGroup animation];
    groupAni.animations = @[posAni,opcAni,bodAni];
    groupAni.duration = 0.6;
    groupAni.fillMode = kCAFillModeForwards;
    groupAni.removedOnCompletion = NO;
    groupAni.autoreverses = YES;
    groupAni.repeatCount = MAXFLOAT;
    groupAni.timingFunction = [CAMediaTimingFunction                 functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.wantButton.layer addAnimation:groupAni forKey:@"groupAni"];
}

- (void)profileAction:(QMUIButton *)btn
{
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:self.model.userId];
    [self.qmui_viewController.navigationController pushViewController:userDetailVC animated:YES];
}

- (void)likeAction:(QMUIButton *)btn
{
    if (!btn.isSelected){
        HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeVideoLike params:@{@"shortVideoCode":self.model.shortVideoCode}];
        [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                NSString *thumbCount = [NSString stringWithFormat:@"%ld",([self.model.thumbUpCount integerValue] + 1)];
                [self.likeButton setTitle:thumbCount forState:UIControlStateNormal];
                self.model.thumbUpCount = thumbCount;
                btn.selected = YES;
            }
        } failure:nil];
    } else {
        HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeVideoUnLike params:@{@"shortVideoCode":self.model.shortVideoCode}];
        [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                NSString *thumbCount = [NSString stringWithFormat:@"%ld",([self.model.thumbUpCount integerValue] - 1)];
                [self.likeButton setTitle:thumbCount forState:UIControlStateNormal];
                self.model.thumbUpCount = thumbCount;
                btn.selected = NO;
            }
        } failure:nil];
    }
}

- (void)followAction:(QMUIButton *)btn
{
    [HLUIHelper followWithUserId:self.model.userId follow:[self.model.isFollow boolValue] callback:^(BOOL isConfirm) {
        btn.hidden = isConfirm;
    }];
}


@end
